#include "stdafx.h"
#include "NotifyWnd.h"

const UINT CNotifyWnd::WM_TASKBAR_CREATED = ::RegisterWindowMessage( _T( "TaskbarCreated" ) );

HRESULT CNotifyWnd::Create( IVdsService *pService, HINSTANCE hInstance, LPCTSTR lpMenuName )
{
	m_hInstance = hInstance;
	m_pService = pService;

	HRESULT hr = listDevices();
	CHECK0( hr );

	// Load the menu
	m_menu = ::LoadMenu( m_hInstance, lpMenuName );
	if( NULL == m_menu )
		return E_INVALIDARG;

	m_submenu = ::GetSubMenu( m_menu, 0 );
	if( NULL == m_submenu )
	{
		::DestroyMenu( m_menu );
		m_menu = NULL;
		return E_INVALIDARG;
	}

	// Load the icons
	iconActive = ::LoadIcon( hInstance, MAKEINTRESOURCE( IDI_TURNOFFHDD ) );
	iconOffline = ::LoadIcon( hInstance, MAKEINTRESOURCE( IDI_TURNONHDD ) );

	// Create the window
	HWND w = __super::Create( NULL, NULL, L"", WS_POPUP );
	if( NULL == w )
		return GetLastHr();

	// Create the tray icon
	ZeroMemory( &m_tnd, sizeof( m_tnd ) );
	m_tnd.cbSize = sizeof( NOTIFYICONDATA );
	m_tnd.hWnd = m_hWnd;
	m_tnd.uCallbackMessage = WM_ICON_NOTIFY;
	m_tnd.uID = IDR_TRAY_MENU;
	m_uCreationFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	m_tnd.uFlags = m_uCreationFlags;
	m_tnd.hIcon = m_bNowOnline ? iconActive : iconOffline;
	wcsncpy_s( m_tnd.szTip, m_strTooltip, 127 );
	m_tnd.szTip[ 127 ] = 0;
	// _tcscpy_s( m_tnd.szInfo, L"Hello world" );
	m_tnd.uVersion = NOTIFYICON_VERSION_4;
	m_tnd.dwInfoFlags = NIIF_INFO | NIIF_NOSOUND;

	if( !Shell_NotifyIcon( NIM_ADD, &m_tnd ) )
	{
		m_tnd.cbSize = 0;
		hr = GetLastHr();
		if( SUCCEEDED( hr ) )
			hr = E_FAIL;
		return hr;
	}

	return S_OK;
}
void CNotifyWnd::updateTooltip()
{
	wcsncpy_s( m_tnd.szTip, m_strTooltip, 127 );
	m_tnd.szTip[ 127 ] = 0;
	m_tnd.hIcon = m_bNowOnline ? iconActive : iconOffline;
	m_tnd.uFlags = NIF_TIP | NIF_ICON;
	Shell_NotifyIcon( NIM_MODIFY, &m_tnd );
}

LRESULT CNotifyWnd::onDestroy( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
	if( m_tnd.cbSize > 0 )
	{
		Shell_NotifyIcon( NIM_DELETE, &m_tnd );
		m_tnd.cbSize = 0;
	}

	if( NULL != m_menu )
	{
		::DestroyMenu( m_menu );
		m_menu = NULL;
		m_submenu = NULL;
	}
	return 0;
}

LRESULT CNotifyWnd::onTaskbarCreated( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
	m_tnd.uFlags = m_uCreationFlags;
	Shell_NotifyIcon( NIM_ADD, &m_tnd );
	return 0;
}

LRESULT CNotifyWnd::onIconNotify( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
	if( LOWORD( lParam ) == WM_RBUTTONUP )
	{
		customizeMenu();
		CPoint pos;
		GetCursorPos( &pos );

		::SetForegroundWindow( m_hWnd );
		if( !::TrackPopupMenu( m_submenu, TPM_RIGHTBUTTON, pos.x, pos.y, 0, m_hWnd, NULL ) )
		{
			HRESULT hr = GetLastHr();
			return 0;
		}

		// BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
		::PostMessage( m_tnd.hWnd, WM_NULL, 0, 0 );

		return 1;
	}

	if( LOWORD( lParam ) == WM_LBUTTONDBLCLK )
	{
		BOOL h;
		if( NULL == m_defaultDrive )
			actConfigure( 0, 0, 0, h );
		else
		{
			customizeMenu();
			actOnline( 0, 0, 0, h );
		}
		return 0;
	}

	return 0;
}